const fs = require("fs");
const path = require("path");

function createDir(url) {
  return new Promise((resolve, reject) => {
    fs.mkdir(path.resolve(__dirname, `./api/${url}`), (err, data) => {
      if (err) {
        resolve("");
      } else {
        resolve(2, data);
      }
    });
  });
}
const mock = function (app) {
  app.use(/\/mockapi/, async (req, res) => {
    // 获取

    const fspath = req.path.split("/");
    console.log(fspath);
    const url = `${path.resolve(
      __dirname,
      `./api/${fspath[1]}-${fspath[2]}`,
    )}.json`;
    const data = await fs.readFileSync(url);
    res.send(data);
  });

  //   app.use(/\/createMock/, async (req, res) => { // 设置

  //     console.log(1,req.body);
  //     const data = JSON.stringify(req.body);
  //     const fspath = req.path.split('/');
  //     // const type = await createDir(fspath[2]);
  //     const url = `${path.resolve(__dirname, `./api/${fspath[2]}-${fspath[3]}`)}.json`;
  //     fs.writeFileSync(url, data);
  //     res.send({ errorCode: null,
  //         errorMsg: null,
  //         result: {records: [], total: 0, size: 100, current: 1, orders: [], searchCount: true, pages: 0},
  //         status: "SUCCESS", });
  //   });
};

module.exports = mock;
